var carts = new Vue({
    el:'#cart',
    data:{
        cart:[],
        allPrice:0,
        numlength:0,
        ifCheck:true,
        ifRemove:false,
        ifRemovecheck:false,
        ifcheckall:true,
        select:[],
        selectlength:0
    },
    methods: {
        getAlldata(){
            axios.get('user.php?act=viewcart&ajax=true').then(e =>{
                // console.log(e)
                const carts = []
                for(let i of e.data){
                    // console.log(i)
                    i.check = this.ifCheck
                    carts.push(i)
                }
                this.cart = carts
                this.calculateTotal()
                this.ifcheckall = this.selectlength == this.cart.length
            })
        },
        editNum(mod,obj,idx){
            const carts = this.cart
            const data = new URLSearchParams()
            const newnum = mod == 'add'? parseInt(obj.goods_num) + 1 : parseInt(obj.goods_num) - 1
            // console.log(newnum)
            // console.log(carts)
            if(mod == 'less' && obj.goods_num == 0){
                alert('再减就没了')
                return false
            }else if(mod == 'add' && parseInt(obj.goods.stock) <= parseInt(obj.goods_num)){
                alert('没有库存')
                return false
            }else{
                data.append('id',obj.goods_id)
                data.append('num',newnum)
                axios.post('user.php?act=editcart&ajax=true',data).then(e =>{
                    // console.log(e)
                    if(e.data == 1){
                        carts[idx].goods_num = newnum
                        this.cart = carts
                        this.calculateTotal()
                        man.freshCart()
                    }else if(e.data == -1){
                        alert('库存不足')
                    }else{
                        alert('操作失败')
                    }
                })
            }
        },
        checkone(v,idx){
            const carts = [...this.cart]
            carts[idx].check = !carts[idx].check
            this.cart = carts
            this.calculateTotal()
            this.ifcheckall = this.selectlength == this.cart.length
            console.log(this.ifcheckall)
        },
        checkall(){
            this.ifcheckall = !this.ifcheckall
            this.cart = this.cart.map(e =>{
                e.check = this.ifcheckall
                return e
            })
            this.calculateTotal()
        },
        removeOne(){
            console.log(this.select)
            const carts = [...this.cart]
            if(this.select != ''){
                const data = new URLSearchParams()
                var ids = []
                for(let i of this.select){
                    ids.push(i.goods_id)
                }
                // console.log(ids.join(','))
                data.append('ids',ids.join(','))
                axios.post('user.php?act=delcart&ajax=true',data).then(r =>{
                    if(r.data == 1){
                        alert('删除成功')
                        this.cart = carts.filter(e => ids.indexOf(e.goods_id) === -1)
                        man.freshCart()
                    }else{
                        alert('删除失败')
                    }
                })
            }else{}
        },
        orderview(){
            if(this.select == ''){
                alert('请选择商品')
            }else{
                var ids = []
                for(let i of this.select){
                    ids.push(i.goods_id)
                }
                var idsx = ids.join(',')
                console.log(idsx)
                window.location.href='user.php?act=npay&gids='+idsx
            }
        },
        calculateTotal(){
            const obj = this.cart.filter(e => e.check)
            // console.log(obj)
            this.select = obj
            this.selectlength = obj.length
            var prices = 0
            var numlengths = 0
            for(var i =0;i<obj.length;i++){
                prices = prices + parseFloat(obj[i].goods_num)*parseFloat(obj[i].goods.price)
                numlengths = numlengths + parseInt(obj[i].goods_num)
            }
            this.numlength = numlengths
            this.allPrice = prices.toFixed(2)
        }
    },
    created() {
        this.getAlldata()
    },
})

$('.changeRemove').click(function(){
    carts.ifRemove = !carts.ifRemove
    if(carts.ifRemove){
        $('.changeRemove').html('取消')
    }else{
        $('.changeRemove').html('管理')
    }
    carts.getAlldata()
})
